<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Ad extends Adminbase
{
    protected $searchFields = 'title';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $where2[] = ['is_del', '=', 0];
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = Db::name('ad')
                ->where($where)
                ->where($where2)
                ->order('create_time desc')
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
                $_list[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
            }
            $total = Db::name('ad')
                ->where($where)
                ->where($where2)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $date1 = explode('--', $data['datetime']);
            $insert_data = array(
                'title' => $data['title'],
                'img_url' => $data['img_url'],
                'start_time' => strtotime($date1[0]),
                'end_time' => strtotime($date1[1]),
                'create_time' => time(),
            );
            $stmt = Db::name('ad')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $date1 = explode('--', $data['datetime']);
            $insert_data = array(
                'id' => $data['id'],
                'title' => $data['title'],
                'img_url' => $data['img_url'],
                'start_time' => strtotime($date1[0]),
                'end_time' => strtotime($date1[1]),
                'create_time' => time(),
            );
            Db::name('ad')
                ->where('id', '=', $data['id'])
                ->update($insert_data);
            $this->success('操作成功！');
        } else {
            $id = $this->request->param('id/d', 0);
            $data = Db::name('ad')->find($id);
            $data['datetime'] = date('Y-m-d H:i:s', $data['start_time']) . ' -- ' . date('Y-m-d H:i:s', $data['end_time']);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    //删除
    public function del()
    {
        $id = $this->request->param('id/d');
        try {
            Db::name('ad')->update([
                'id' => $id,
                'is_del' => 1
            ]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('ad')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }
}